package gpl.scotlandyard.fugitive.hint.result;

import gpl.androidcommons.R;
import gpl.androidcommons.scotlandyard.ui.SYColors;
import gpl.scotlandyard.beans.basics.Node;
import gpl.scotlandyard.services.PathFinder;
import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PathResultGridAdapter extends BaseAdapter {

  private final Activity context;

  private final PathFinder pathFinder;

  public PathResultGridAdapter(Activity context, PathFinder pathFinder) {
    this.context = context;
    this.pathFinder = pathFinder;
    notifyDataSetChanged();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    int currentPath = position / pathFinder.getLongestSafePathLength();
    int currentNode = position % pathFinder.getLongestSafePathLength();
    Node node = pathFinder.getSafePaths().get(currentPath).get(currentNode);
    TextView textView = (TextView) convertView;
    if (textView == null) {
      textView = (TextView) context.getLayoutInflater().inflate(R.layout.text_view_rounded, null);
    }
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    textView.setTypeface(Typeface.MONOSPACE);
    textView.setGravity(Gravity.CENTER);
    GradientDrawable bgShape = (GradientDrawable) textView.getBackground();
    bgShape.setColor(SYColors.color4dNode(node).getRgbColor());
    textView.setText(node.getNumber() + "");
    return textView;
  }

  @Override
  public boolean areAllItemsEnabled() {
    return false;
  }

  @Override
  public boolean isEnabled(int position) {
    return false;
  }

  @Override
  public int getCount() {
    return pathFinder.getLongestSafePathLength() * pathFinder.getLongestSafePathsCount();
  }

  @Override
  public Object getItem(int position) {
    int currentPath = position / pathFinder.getLongestSafePathLength();
    int currentNode = position % pathFinder.getLongestSafePathLength();
    return pathFinder.getSafePaths().get(currentPath).get(currentNode);
  }

  @Override
  public long getItemId(int position) {
    int currentPath = position / pathFinder.getLongestSafePathLength();
    int currentNode = position % pathFinder.getLongestSafePathLength();
    return pathFinder.getSafePaths().get(currentPath).get(currentNode).hashCode();
  }
}
